package teatime.core.web.action.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

/**
 * @author Gu
 * @since 2008-04-16
 * @version $Revision: 1.1 $
 */
public class BaseAction extends ActionSupport implements Preparable {

	private static final long serialVersionUID = -3154621653093611355L;
	
	/**
	 * 
	 */
	private HttpServletResponse response;
	
	/**
	 * 
	 */
	private HttpServletRequest request;

	/**
	 * 
	 */
	public void prepare() {
		// 清除错误信息
		clearErrorsAndMessages();
	}

	/**
	 * 
	 * @return
	 */
	protected HttpServletResponse getResponse() {
		if (response == null) {
			ActionContext ctx = ActionContext.getContext();
			response = (HttpServletResponse) ctx
					.get(ServletActionContext.HTTP_RESPONSE);
		}
		return response;
	}

	/**
	 * 
	 * @return
	 */
	protected HttpServletRequest getRequest() {
		if (request == null) {
			ActionContext ctx = ActionContext.getContext();
			request = (HttpServletRequest) ctx
					.get(ServletActionContext.HTTP_REQUEST);
		}
		return request;
	}
	
	/**
	 * 
	 * @param result
	 * @throws Exception
	 */
	protected void setResponse(String result) throws Exception {
		getResponse().setContentType("text/html; charset=UTF-8");
		getResponse().getWriter().print(result);
	}
}
